<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{$id}</title>
<include file="Public:header" />
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerDateEditor.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerComboBox.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerCheckBox.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerButton.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerRadio.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerSpinner.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/jquery-validation/jquery.validate.min.js" type="text/javascript"></script> 
    <script src="__PUBLIC__/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
    <script src="__PUBLIC__/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
   
    <script type="text/javascript"> 
        var groupicon = "__PUBLIC__/lib/ligerUI/skins/icons/communication.gif";
        $(function ()
        {            
           
            //创建表单结构
           var mainform = $("#deptform");
            mainform.ligerForm({
                inputWidth: 170, labelWidth: 90, space: 40,
                fields: [
                { name: "id", type: "hidden" },
                { display: "名称", name: "name", newline: true, type: "text", validate: { required: true,minlength:3 }, group: "基础信息", groupicon: groupicon}, 
                { display: "上级", name: "pid", newline: true, type: "select", comboboxName: "Name", options: { valueFieldID: "pid",url:"__URL__/getDeptNameList" } },
                { display: "状态", name: "status", newline: true, type: "select", comboboxName: "Name2", options: {valueFieldID: "status",data:[{text:"启用",id:"1"},{text:"禁用",id:"0"}]}},
                { display: "备注", name: "deptDesc", newline: true, type: "textarea",width:200,space:30}
                ],
                buttons: [{ text: '确定',type:"submit", icon: 'ok',onclick: function (item) {save("__URL__/edit/");} },
                          { text: '取消',type:"", onclick: function (item) { window.parent.mydialog.close(); } }]
            });
            var id={$id};
            if(id!=0){
            	$.get('__URL__/getById/id/'+id,function(data){
                    var currentdata=eval("("+data+")");
               	 loadform(mainform,{},currentdata);
                });
            	//saveurl="__URL__/Update/";
            }
           
            	
            $.metadata.setType("attr", "validate");
            
            var validator = $("#deptform").validate({
                //调试状态，不会提交数据的
                debug: true,
                errorPlacement: function (lable, element)
                {

                    if (element.hasClass("l-textarea"))
                    {
                        element.addClass("l-textarea-invalid");
                    }
                    else if (element.hasClass("l-text-field"))
                    {
                        element.parent().addClass("l-text-invalid");
                    }
                    $(element).removeAttr("title").ligerHideTip();
                    $(element).attr("title", lable.html()).ligerTip();
                },
                success: function (lable)
                {
                    var element = $("#" + lable.attr("for"));
                    if (element.hasClass("l-textarea"))
                    {
                        element.removeClass("l-textarea-invalid");
                    }
                    else if (element.hasClass("l-text-field"))
                    {
                        element.parent().removeClass("l-text-invalid");
                    }
                    $(element).removeAttr("title").ligerHideTip();
                },
                submitHandler: function ()
                {
                    alert("Submitted!");
                }
            }); 
        }); 
        function save(url)
        {
        	// $.ligerDialog.alert(url);
        	$.post(url,$("#deptform").serialize(),function(data){
        		LG.showMessage(data, function () { 
        			window.parent.grid.loadData();
                    window.parent.mydialog.close(); //要先刷新表格,再关闭窗口,否则表格不会刷新 
                });
                 
			});

        }
        function loadform(mainform, options,data)
        {
        	var preID = options.preID || "";
            //根据返回的属性名，找到相应ID的表单元素，并赋值
            for (var p in data)
            {
                var ele = $("[name=" + (preID + p) + "]", mainform);
                //针对复选框和单选框 处理
                if (ele.is(":checkbox,:radio"))
                {
                    ele[0].checked = data[p] ? true : false;
                }
                else
                {
                    ele.val(data[p]);
                }
            }
            //下面是更新表单的样式
          var managers = $.ligerui.find($.ligerui.controls.Input);
            for (var i = 0, l = managers.length; i < l; i++)
            {
                //改变了表单的值，需要调用这个方法来更新ligerui样式
              var o = managers[i];
                o.updateStyle();
                if (managers[i] instanceof $.ligerui.controls.TextBox)
                    o.checkValue();
            }
        }//end function loadform
    </script> 
</head>

<body style="padding:10px"> 
    <form id="deptform" >
	</form>
</body> 
</html>